package common

import (
	"backend/config"
	"context"
	"errors"
	"github.com/cloudwego/eino-ext/components/model/openai"
	"github.com/cloudwego/eino/schema"
	"time"
)

var model *openai.ChatModel

func init() {
	// 初始化模型
	var err error
	var temper float32 = 0
	var maxTokens int = 4096
	model, err = openai.NewChatModel(context.Background(), &openai.ChatModelConfig{
		APIKey:      config.Configs.LLM.APIKey, //
		BaseURL:     config.Configs.LLM.BaseURL,
		Timeout:     1000 * time.Second,
		Model:       config.Configs.LLM.Model, // required
		Temperature: &temper,
		MaxTokens:   &maxTokens,
	})
	if err != nil {
		panic(err)
	}
}

func Interact(messages []*schema.Message) (*schema.Message, error) {
	ctx := context.Background()

	// 准备消息
	if messages == nil {
		return &schema.Message{}, errors.New("消息不能为空")
	}

	// 生成回复
	response, err := model.Generate(ctx, messages)
	if err != nil {
		return &schema.Message{}, err
	}

	// 处理回复
	// println(response.Content)

	return response, nil
}
